﻿using System.Linq;
using Migration.Models;
using System.Collections.Generic;
namespace Migration
{
    public partial class TransactionEntities
    {
        private RealestateDataContext dataContext = new RealestateDataContext();

        public void Add(TRANSACTION item)
        {
            dataContext.TRANSACTIONs.InsertOnSubmit(item);
            dataContext.SubmitChanges();
        }

        public void Update(TRANSACTION item)
        {
            var updateItem = (from c in dataContext.TRANSACTIONs where c.TransactionId == item.TransactionId select c).FirstOrDefault();
            dataContext.SubmitChanges();
        }

        public void Delete(int id)
        {
            var item = (from c in dataContext.TRANSACTIONs where c.TransactionId == id select c).FirstOrDefault();
            dataContext.TRANSACTIONs.DeleteOnSubmit(item);
            dataContext.SubmitChanges();
        }

        public void DeleteAll()
        {
            var items = (from c in dataContext.TRANSACTIONs select c);
            foreach (var item in items)
            {
                dataContext.TRANSACTIONs.DeleteOnSubmit(item);
                dataContext.SubmitChanges();
            }
        }
        
        public TRANSACTION GetById(int id)
        {
            var item = (from c in dataContext.TRANSACTIONs where c.TransactionId == id select c).FirstOrDefault();
            return item;
        }

        public List<TRANSACTION> GetAll()
        {
            return dataContext.TRANSACTIONs.OrderByDescending(c => c.TransactionId).ToList();
        }

        public List<TRANSACTION> GetTransactions()
        {
            return dataContext.TRANSACTIONs.Where(c => c.Form == true).OrderByDescending(c => c.TransactionId).ToList();
        }

        public List<TRANSACTION> GetLoans()
        {
            return dataContext.TRANSACTIONs.Where(c => c.Form != true).OrderByDescending(c => c.TransactionId).ToList();
        }

        public List<TRANSACTION> GetByClientId(int clientId)
        {
            return dataContext.TRANSACTIONs.Where(o => o.ClientId == clientId).ToList();
        }
    }
}